﻿using ScratchNet.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace ScratchNet
{
    public class SetDutyCycleByInput : Expression
    {
        public override string ReturnType => "any";
        private static List<string> IOOperators = new List<string>();
        public string IOOperator { get; set; }

        public Expression Value { get; set; }

        public SetDutyCycleByInput()
        {

        }
        public SetDutyCycleByInput(IOBoardContent ioBoardContent)
        {
            // 初始化个数
            if (!IOOperators.Any() && ioBoardContent.IOTypeCountDict.TryGetValue(LinkIOTypeEmParaDLL.PWMDuty_HD, out var nums))
            {
                for (int i = 1; i <= nums.Count; i++)
                {
                    IOOperators.Add($"PWM_{i}");
                }
            }
            IOOperator = IOOperators.FirstOrDefault();
        }
        protected override Completion ExecuteImpl(ExecutionEnvironment enviroment)
        {
            var c = Value.Execute(enviroment);
            if (!c.IsValue)
                return c;
            c.ExecStatementStr = SetValue(c.ReturnValue);
            return c;
        }
        public override Descriptor Descriptor
        {
            get
            {
                Descriptor desc = new Descriptor();
                desc.Add(new TextItemDescriptor(this, "EDS2000 设置"));
                desc.Add(new SelectionItemDescriptor(this, "IOOperator", IOOperators,IOOperators));
                desc.Add(new TextItemDescriptor(this, " 占空比 "));
                desc.Add(new ExpressionDescriptor(this, "Value", "any"));

                return desc;
            }
        }

        public override string Type
        {
            get { return "SetDutyCycleByInput"; }
        }

        private string SetValue(object val)
        {
            //调接口
            return ($"SetDutyCycleByInput.SetValue({IOOperator},{val});");
        }
    }
}
